// 拓展zeptojs
(function($){
  $.extend($.fn, {
    animateCss: function (animationName) {
      var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
      $(this).addClass('animated ' + animationName).one(animationEnd, function() {
          $(this).removeClass('animated ' + animationName);
      });
    },
    prehandler: {
      touchstartPos: 0, 
      touchendPos: 0,
      handler: function (event) {
        // Object.getOwnPropertyNames(event).sort().forEach(function (val) {console.log(val, '\n')}); // 打印js所有属性列表的方法
        if (event.type == 'touchstart') {
          this.touchstartPos = event.targetTouches[0].screenY
          return false
        } else if (event.type == 'touchend') {
          this.touchendPos = event.changedTouches[0].screenY 
          return this.touchstartPos == this.touchendPos
        }
        return true
      }
    }
  })
})(Zepto);

/**
 * 获取user-agent
 * @return {[type]} [description]
 */
function getUserAgent() {
  const ua = navigator.userAgent
  if (ua.indexOf('PTime.IOSWeb') > -1) return 'ptimeios'
  if (ua.indexOf('MicroMessenger') > -1) return 'wechat'
  if (ua.indexOf('Android') > -1) return 'ptimeandroid'
  if (ua.indexOf('MQQBrowser') > -1) return 'mqq'
  if (ua.indexOf('iPhone') > -1) return 'iphone'
  return 'other'
}

/**
 * 根据不同的设备监听不同的点击事件
 * @return {[type]} [description]
 */
function getListenEvent() {
	if (getUserAgent() === 'other') return 'click'
	return 'touchstart touchend'
}

/**
 * 构建item
 * @param  {[type]} laf [description]
 * @return {[type]}     [description]
 */
function buildLafItem (laf, isApp, ua, token) {
  var itemDiv = $('<div></div>').addClass('lafitem').attr('id', laf._id)
  if (laf.isMine) {
    itemDiv.addClass('lafitem-me mcolor')
  }
  var a = $('<a></a>').attr('href', '/laf/detail/' + laf._id)
  if (laf.images.length > 0) {
    a.append($('<img>').addClass('image').attr('src', laf.images[0]))
  }
  a.append($('<p></p>').addClass('title').append(laf.title))
  a.append($('<p></p>').addClass('content-list').append('<i class="icon iconfont">&#xe61c;</i> ' + laf.time))
  a.append($('<p></p>').addClass('content-list').append('<i class="icon iconfont">&#xe630;</i> ' + laf.position))
  itemDiv.append(a)
  itemDiv.append($('<div></div>').addClass('clear'))
  return itemDiv
}

/**
 * 自动加载
 * @param  {Boolean} isApp [description]
 * @param  {[type]}  ua    [description]
 * @return {[type]}        [description]
 */
function autoLoadMore (isApp, ua, type, token) {
  $(window).scroll(function () {
    var scrollTop = $(this).scrollTop()
    var scrollHeight = $(document).height()
    var windowHeight = $(this).height()
    if (scrollTop + windowHeight == scrollHeight) {
      var url = location.origin + (Number(type) > 0 ? '/laf/found/more' : '/laf/lost/more')
      $.ajax({
        url: url,
        type: 'get',
        data: {
          id: $('.lafitem').last().attr('id'),
          limit: 5
        },
        success: function (data) {
          if (Number(data.error) == 0) {
            if (data.result && data.result.length > 0) {
              data.result.forEach(function (laf) {
                $('section').append(buildLafItem(laf, isApp, ua, token))
              })
            } else {
              toast('没有更多了')
              $('.loading').css('display', 'none')
            }
          } else {
            toast(data.message)
          }
        },
        error: function (data) {
          toast(data.mesage)
        }
      })
    }
  })
}

/**
 * js bridge 打电话
 * @param  {[type]} e [description]
 * @return {[type]}   [description]
 */
function jbPhoneCall (ua, number) {
  var payload = {
    type: 'PhoneCall',
    content: {
      number: number
    }
  };
  if (ua == 'ptimeios') {
    window.webkit.messageHandlers.webViewApp.postMessage(payload)
  }
}

//返回顶部
$("#ptime-top-a").on("click", function() {
    javascript: scroll(0, 0);
});
